package component.show.control
{
	import common.core.GSprite;
	import common.manager.Resource;
	import common.utils.ScaleBitmap;
	
	import component.ComponentLib;
	import component.show.containers.Image;
	import component.show.containers.SimpleBtn;
	import component.show.containers.UIFrame;
	import component.untils.DeserializeUIXML;
	
	import fl.containers.UILoader;
	import fl.controls.*;
	import fl.data.DataProvider;
	
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;

	public class UISprite extends GSprite
	{	
		private var m_collection : Object;	
		private var m_uiXMLName : String;
		
		private var m_background : ScaleBitmap;
		
		public function UISprite(UIXMLName : String = "",XMLURL:String="")
		{
			m_collection = new Object;	
			
			m_uiXMLName = UIXMLName;
			
			if(UIXMLName)
			{
				prase();
			}
		}
		
		public function setBitmapBackground(bitmapData:BitmapData,scale9Grid : Rectangle = null) : void
		{
			if(!m_background)
			{
				m_background = new ScaleBitmap(bitmapData);
			}
			
			m_background.bitmapData = bitmapData;
			
			if(scale9Grid)
			{
				m_background.scale9Grid = scale9Grid;
			}
			
			if(!this.contains(m_background))
			{
				this.addChildAt(m_background,0);
			}
		}
		
		public function clearBitmapBackground() : void
		{
			if(m_background)
			if(this.contains(m_background))
			{
				this.removeChild(m_background);
			}
			
			m_background = null;
		}
		
		public override function set width(value:Number):void
		{
			super.width = value;
			
			if(m_background)
			{
				m_background.width= value;
			}
		}
		
		public override function set height(value:Number):void
		{
			super.height = value;
			
			if(m_background)
			{
				m_background.height = value;
			}
		}
		
		public function get collection() : Object
		{
			return m_collection;
		}
		
		public function set collection(value : Object) : void
		{
			m_collection = value;
		}
		
		/**
		 * 解析XMLUI 
		 */		
		public function prase() : void
		{
			var rootXML : XML = Resource.getXML(m_uiXMLName,false);
			
			var deserializeUIXML : DeserializeUIXML = new DeserializeUIXML;
			deserializeUIXML.recursionXML(rootXML,this);
			
			m_collection = deserializeUIXML.collection;
			deserializeUIXML.destory();
			
			onRegister();
		}
		
		public function onRegister() : void
		{
			
		}
		 
		 public function validateNow() : void
		 {
			 this.addEventListener(Event.ENTER_FRAME,validateUpdate);
		 }
		 
		 private function validateUpdate(evt : Event) : void
		 {
			 this.removeEventListener(Event.ENTER_FRAME,validateUpdate);
		 }
	}
}